package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Sound;
	
	import nl.sanity.debug.LogConsole;
	import nl.sanity.events.QueueLoaderEvent;
	import nl.sanity.net.LoadItem;
	import nl.sanity.net.QueueLoader;
	
	
	public class QueueLoaderExample extends Sprite
	{
		public static const NAME		: String	= "QueueLoaderExample";

		
		protected var _queueLoader		: QueueLoader;
		
		// --- CONSTRUCTOR ----------------------------------------

		public function QueueLoaderExample()
		{
			stage ? init() : addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		// --- PRIVATE FUNCTIONS ----------------------------------------

		private function init( event:Event = null ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, init );
			
			// Create an instance of the QueueLoader. 
			// Pass 'true' as argument to see log messages inside the FireBug console.
			_queueLoader				= new QueueLoader( true );
			
			_queueLoader.addEventListener( QueueLoaderEvent.ON_LOAD_START, onQueueLoaderStart );
			_queueLoader.addEventListener( QueueLoaderEvent.ON_LOAD_COMPLETE, onQueueLoaderComplete );
			_queueLoader.addEventListener( QueueLoaderEvent.ON_ITEM_LOAD_START, onItemLoadStart );
			_queueLoader.addEventListener( QueueLoaderEvent.ON_ITEM_LOAD_COMPLETE, onItemLoadComplete );
			_queueLoader.addEventListener( QueueLoaderEvent.ON_ITEM_LOAD_ERROR, onItemLoadError );
			
			// items can be added using just the url. in that case the id will equal the url
			_queueLoader.add( "someImage.jpg" );
			_queueLoader.addItem( new LoadItem( "assets/non-existent-image.png", "non-existent-image" ) );
			_queueLoader.addItem( new LoadItem( "assets/adobe.png", "logo" ) );
			//_queueLoader.addItem( new LoadItem( "UIComponentExample.swf", "componentswf" ) );
			_queueLoader.addItem( new LoadItem(  "assets/ambience.mp3", "ambientsound" ) );
			_queueLoader.addItem( new LoadItem( "assets/test.xml", "testxml" ) );
			
			_queueLoader.start();
		}
		
		private function addItems():void
		{	
			var bmp:Bitmap = _queueLoader.getBitmap( "logo" );
			if ( bmp ) addChild( bmp );
			
			//var mc:DisplayObject = _queueLoader.getMovieClip( "componentswf" );
			//if ( mc ) addChild( mc );
			
			var snd:Sound = _queueLoader.getSound( "ambientsound" );
			if ( snd ) snd.play();
			
			var xml:XML = _queueLoader.getXML( "testxml" );
			if ( xml ) LogConsole.log( xml );
		}
		
		private function onQueueLoaderStart( event:QueueLoaderEvent ):void
		{
			LogConsole.log( NAME + ".onQueueLoaderStart" );
		}
		
		private function onQueueLoaderComplete( event:QueueLoaderEvent ):void
		{
			LogConsole.log( NAME + ".onQueueLoaderComplete" );
			
			addItems();
		}
		
		private function onItemLoadStart( event:QueueLoaderEvent ):void
		{
			LogConsole.log( NAME + ".onItemLoadStart" );
		}
		
		private function onItemLoadComplete( event:QueueLoaderEvent ):void
		{
			LogConsole.info( NAME + ".onItemLoadComplete" );
		}
		
		private function onItemLoadError( event:QueueLoaderEvent ):void
		{
			LogConsole.error( NAME + ".onItemLoadError | error: " + event.data.error );
		}
	}
}